package com.tangsm.spring.boot.websocket.config;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * Redisson分布式工具配置
 *
 * @author tangsm
 * @see <a href="https://github.com/redisson/redisson">Redisson分布式工具 GitHub主页</a>
 */
@EnableCaching
@Configuration
public class RedissonConfig {
    @Autowired
    private RedisProperties redisProperties;

    /**
     * Redisson 客户端配置
     *
     * @return Redisson客户端实例
     */
    @Bean(destroyMethod = "shutdown")
    public Redisson redisson() {
        Config config = new Config();

        // 哨兵模式
        if (redisProperties.getSentinel() != null && !redisProperties.getSentinel().getNodes().isEmpty()) {
            SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
            sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster());
            List<String> sentinelAddress = new ArrayList<>();
            for (String node : redisProperties.getCluster().getNodes()) {
                sentinelAddress.add("redis://" + node);
            }
            sentinelServersConfig.setSentinelAddresses(sentinelAddress);
            if (StringUtils.isNotBlank(redisProperties.getSentinel().getPassword())) {
                sentinelServersConfig.setSentinelPassword(redisProperties.getSentinel().getPassword());
            }
        }
        // 集群模式
        else if (redisProperties.getCluster() != null && !redisProperties.getCluster().getNodes().isEmpty()) {
            ClusterServersConfig clusterServersConfig = config.useClusterServers();
            List<String> clusterNodes = new ArrayList<>();
            for (String node : redisProperties.getCluster().getNodes()) {
                clusterNodes.add("redis://" + node);
            }
            clusterServersConfig.setNodeAddresses(clusterNodes);
            if (StringUtils.isNotBlank(redisProperties.getPassword())) {
                clusterServersConfig.setPassword(redisProperties.getPassword());
            }
        }
        // 单机模式
        else {
            SingleServerConfig singleServerConfig = config.useSingleServer();
            singleServerConfig.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort());
            if (StringUtils.isNotBlank(redisProperties.getPassword())) {
                singleServerConfig.setPassword(redisProperties.getPassword());
            }
        }
        return (Redisson) Redisson.create(config);
    }
}
